﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HomeWork5;
using HomeWork8_WinFrom;

namespace HomeWork8_WinForm
{
    public partial class MainForm : Form
    {
        public int OrderID { get; set; }
        public string ClientName { get; set; }
        new public string ProductName { get; set; }
        public OrderItem orderItem1 = new OrderItem(new Product("apple", 5), 3);
        public OrderItem orderItem2 = new OrderItem(new Product("pear", 6), 4);
        public OrderItem orderItem3 = new OrderItem(new Product("peach", 4), 6);
        public Order order1 = new Order(1, new Client ("wang"));
        public Order order2 = new Order(2, new Client("zhang"));
        public OrderService orderService = new OrderService();
        
        
        public MainForm()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            order1.AddOrderItem(orderItem1);
            order1.AddOrderItem(orderItem2);
            order2.AddOrderItem(orderItem3);
            orderService.AddOrder(order1);
            orderService.AddOrder(order2);
            orderBindingSource.DataSource = orderService.orders;
           
        }

        private void cbxQueryWay_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtQueryContent.DataBindings.Clear();
            switch (cbxQueryWay.SelectedItem)
            {
                case "FindByOrderID":
                    txtQueryContent.DataBindings.Add("Text", this, "OrderID");
                    break;
                case "FindByClientName":
                    txtQueryContent.DataBindings.Add("Text", this, "ClientName");
                    break;
                case "FindByProductName":
                    txtQueryContent.DataBindings.Add("Text", this, "ProductName");
                    break;

            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            switch (cbxQueryWay.SelectedItem)
            {
                case "FindByOrderID":
                    orderBindingSource.DataSource = orderService.FindByOrderID(OrderID);
                    break;
                case "FindByClientName":
                    orderBindingSource.DataSource = 
                        orderService.FindByClientName(ClientName);
                    break;
                case "FindByProductName":
                    orderBindingSource.DataSource = 
                        orderService.FindByProductName(ProductName);
                    break;

            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddOrderForm addOrderForm = new AddOrderForm(orderService);
            addOrderForm.Text = "添加订单";
            addOrderForm.ShowDialog();
            orderBindingSource.ResetBindings(false);
        }

        private void gbxQuery_Enter(object sender, EventArgs e)
        {

        }

        private void btnDelate_Click(object sender, EventArgs e)
        {
            Order order = new Order();
            order.OrderID=(int)dgvOrder.Rows[dgvOrder.
                 CurrentRow.Index].Cells[0].Value;
            orderService.
                 DeleteOrder(order);
            orderBindingSource.ResetBindings(false);
         
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            Order orderWrong = new Order();
            Order orderRight = new Order();
            orderWrong.OrderID = (int)dgvOrder.Rows[dgvOrder.
                 CurrentRow.Index].Cells[0].Value;
            OrderModifyForm orderModifyForm = new OrderModifyForm(orderRight);
            orderModifyForm.Text = "修改订单";
            orderModifyForm.ShowDialog();
            orderService.ModifyOrder(orderWrong,
                 orderRight);
            orderBindingSource.ResetBindings(false);
            

        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            orderService.Import();
            orderBindingSource.ResetBindings(false);

        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            orderService.Export();
            orderBindingSource.ResetBindings(false);
        }
    }
}
